<?php
namespace AdminBundle\Repository;


use Symfony\Bridge\Doctrine\Security\User\UserLoaderInterface;
use Symfony\Component\Security\Core\User\UserProviderInterface;

class AuthUserRepository extends BaseRepository implements UserLoaderInterface
{
    public function pageQuery($_params, $page)
    {
        $sql = <<<EOF
select 
    id, last_login as lastLogin, last_ip as lastIp, username, 
    email, is_enabled as isEnabled, ctime, create_user as createUser
from auth_user 
where 1=1 
EOF;
        $params = array();
        if (!empty($_params)) {
            /*            if (isset($_params['starttime'])) {
                            $sql .= ' and ctime > :starttime ';
                            $params['starttime'] = $_params['starttime'];
                        }
                        if (isset($_params['endtime'])) {
                            $sql .= ' and ctime < :endtime ';
                            $params['endtime'] = $_params['endtime'];
                        }*/
            if (isset($_params['keyword']) && !empty($_params['keyword'])) {
                $sql .= ' and username like :keyword ';
                $params['keyword'] = '%' . $_params['keyword'] . '%';
            }
        }
        $sql .= ' ORDER BY id DESC';

        return $this->pageWithRawSql($sql, $params, $page);
    }

    public function loadUserByUsername($username)
    {
        $user = $this->createQueryBuilder('u')
            ->where('u.username = :username OR u.email = :email')
            ->setParameter('username', $username)
            ->setParameter('email', $username)
            ->getQuery()
            ->getOneOrNullResult();
        return $user;
    }

    /**
     * 加载用户的组
     * @param int $groupId
     * @return bool
     * @throws \Exception
     */
    public function loadGroup($userId = 0)
    {
        $sql = "
SELECT 
    g.id,
    CONCAT(g.name, '|', note) AS name,
    IF(lug.id, 'selected', '') AS selected
FROM
    auth_group AS g
        LEFT JOIN
    auth_link_user_groups AS lug ON g.id = lug.group_id AND lug.user_id = :user_id
WHERE
    g.is_enabled = 1
";
        $params = ['user_id' => $userId];
        return $this->rawQuery($sql, $params);
    }

    /**
     * 关联表中用户与组联合主键
     * @param $roleId
     * @param $permissionId
     * @return bool
     * @throws \Exception
     */
    public function loadByUserIdAndGroupId($userId, $groupId)
    {
        $sql = "select * from auth_link_user_groups where user_id = :user_id and group_id = :group_id";
        $params = ['user_id' => $userId, 'group_id' => $groupId];
        return $this->rawQuery($sql, $params);
    }

    public function revokeUserGroup($userId, $groupIds = [])
    {
        $params = ['user_id' => $userId];
        $sql = "delete from auth_link_user_groups where user_id = :user_id ";
        if (!empty($groupIds)) {
            $sql .= " and group_id not in (" . implode(",", $groupIds) . ")";
        }

        return $this->rawExecute($sql, $params);

    }

}
